package com.plum.core;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class Plum {
	public final static String PLUM_ALLOW = "allow";
	public final static String PLUM_DENY = "deny";
	public final static String PLUM_ROLES = "roles";
	public final static String PLUM_INI_PATH = "plum.ini";
	public final static String PLUM_LOGIN_URL = "loginUrl";
	public final static String PLUM_UNAUTHORIZED_URL = "unauthorizedUrl";
	public final static String PLUM_REALM = "realm";
	public final static String PLUM_DEFAULT_ENCODING = "UTF-8";
	public static String DEFAULT_LOGIN_URL = "/login";
	public static String DEFAULT_UNAUTHORIZED_URL = "/unauthorized";
	public static String SESSION_USER_KEY = "PLUM_SESSION_USER_KEY";
	public static String SESSION_ROLES_KEY = "PLUM_SESSION_ROLES_KEY";
	public static String REALM_CLASS = "";
	public static boolean atAfterStartInit = false;
	protected static Map<String, String> allowMap = new LinkedHashMap<String, String>();
	protected static Map<String, Set<String>> roleMap = new LinkedHashMap<String, Set<String>>();
	protected static Map<String, String> urlRegixCacheMap = new LinkedHashMap<String, String>();
	protected static String ctx;

	public static String getCtx() {
		return ctx;
	}

	protected void setCtx(String contextPath) {
		Plum.ctx = contextPath;
	}
	
	public static Map<String, String> getUrlRegixCacheMap() {
		return urlRegixCacheMap;
	}

	public static void putUrlRegixCacheMap(String key, String val) {
		urlRegixCacheMap.put(key, val);
	}

	public static Map<String, String> getAllowMap() {
		return allowMap;
	}

	public static void setAllowMap(Map<String, String> map) {
		allowMap.clear();
		allowMap.putAll(parseMap(map));
	}

	public static void putAllowMap(Map<String, String> map) {
		allowMap.putAll(parseMap(map));
	}

	public static void putAllowMap(String key, String val) {
		allowMap.put(getCtx()+key, val);
	}

	public static Map<String, Set<String>> getRoleMap() {
		return roleMap;
	}

	public static void setRoleMap(Map<String, Set<String>> map) {
		roleMap.clear();
		roleMap.putAll(parseSetStringMap(map));
	}

	public static void putRoleMap(Map<String, Set<String>> map) {
		roleMap.putAll(parseSetStringMap(map));
	}

	public static void putRoleMap(String key, Set<String> val) {
		roleMap.put(getCtx()+key, val);
	}

	public static void addRoleMap(String key, String val) {
		String realKey = getCtx()+key;
		if (roleMap.containsKey(realKey)) {
			roleMap.get(realKey).add(val);
		} else {
			Set<String> set = new HashSet<String>();
			set.add(val);
			putRoleMap(realKey, set);
		}
	}
	
	/**
	 * 为map的key添加contextPath
	 * @param map
	 * @return
	 */
	protected static Map<String,String> parseMap(Map<String,String> map){
		Map<String,String> ret = new HashMap<String, String>();
		if(map != null){
			for(String key:map.keySet()){
				ret.put(getCtx()+key, map.get(key));
			}
		}
		return ret;
	}
	
	/**
	 * 为map的key添加contextPath
	 * @param map
	 * @return
	 */
	protected static Map<String,Set<String>> parseSetStringMap(Map<String,Set<String>> map){
		Map<String,Set<String>> ret = new HashMap<String, Set<String>>();
		if(map != null){
			for(String key:map.keySet()){
				ret.put(getCtx()+key, map.get(key));
			}
		}
		return ret;
	}
	
	/**
	 * 是否在应用启动后才开始初始化
	 * 注意:当为true时,刚开始不会执行realm中的roleExtMap、allowExtMap,需要自己在启动成功后手动调用方法
	 * @return
	 */
	public static boolean isAfterStartInit() {
		return atAfterStartInit;
	}
	
}
